package gframework.classe;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.nio.channels.FileChannel;

public class Arquivo {

    private int lineCount = 0;
    private BufferedReader input = null;
    private PrintWriter out = null;

    /**
     *
     * @param i_nome nome do arquivo
     * @param i_tipo "w" para escrita, "wo" para escrita de objeto, "r" para
     * leitura
     * @throws java.lang.Exception
     */
    public Arquivo(String i_nome, String i_tipo) throws Exception {
        if (i_tipo.toLowerCase().equals("r")) {
            input = new BufferedReader(new InputStreamReader(new FileInputStream(i_nome)));

            lineCount = 0;

            while (input.ready()) {
                input.readLine();

                lineCount++;
            }

            input = new BufferedReader(new InputStreamReader(new FileInputStream(i_nome)));

        } else if (i_tipo.toLowerCase().equals("w")) {
            out = new PrintWriter(new FileWriter(i_nome));

        } else if (i_tipo.toLowerCase().equals("a")) {
            out = new PrintWriter(new FileWriter(i_nome, true));
        }
    }

    public int getLineCount() {
        return lineCount;
    }

    public boolean ready() throws Exception {
        return input.ready();
    }

    public String readLine() throws Exception {
        return input.readLine();
    }

    public void write(String i_texto) throws Exception {
        if (!i_texto.equals("")) {
            out.println(i_texto);
        }
    }

    public void close() throws Exception {
        if (input != null) {
            input.close();
        }

        if (out != null) {
            out.close();
        }
    }

    public void renameTo(String i_nome, String i_novonome) throws Exception {
        File oArquivo = new File(i_nome.replace("\\", "\\\\"));
        File oNovo = new File(i_novonome.replace("\\", "\\\\"));

        if (oNovo.exists()) {
            oNovo.delete();
        }

        oArquivo.renameTo(oNovo);
    }

    public static void delete(String i_arquivo) {
        File file = new File(i_arquivo);
        file.delete();
    }

    public static void copyFile(String i_arquivoOrigem, String i_arquivoDestino) throws IOException {
        File arquivoOrigem = new File(i_arquivoOrigem);
        File arquivoDestino = new File(i_arquivoDestino);

        if (arquivoDestino.exists()) {
            arquivoDestino.delete();
        }

        FileChannel sourceChannel = null;
        FileChannel destinationChannel = null;

        try {
            sourceChannel = new FileInputStream(arquivoOrigem).getChannel();
            destinationChannel = new FileOutputStream(arquivoDestino).getChannel();
            sourceChannel.transferTo(0, sourceChannel.size(), destinationChannel);

        } finally {
            if (sourceChannel != null && sourceChannel.isOpen()) {
                sourceChannel.close();
            }

            if (destinationChannel != null && destinationChannel.isOpen()) {
                destinationChannel.close();
            }
        }
    }
}